/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatserver;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tiphanie
 */
public class ChatServer {

    public static DatabaseConnection database = new DatabaseConnection();
    public static List<OnlineUser> users = Collections.synchronizedList(new ArrayList<OnlineUser>());
    public static HashMap<Integer, Conversation> conversations = new HashMap<Integer, Conversation>(); 
    
    
    public static OnlineUser onlineUserForName(String username) {
        for(int i = 0; i < users.size(); i++) {
            if( users.get(i).username.equals(username) )
                return users.get(i);
        }
        return null;
    }
    
    public static boolean checkUsernameOnline(String username) {
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).username.equals(username)) {
                return true;
            }
        }
        return false;
    }

    public static boolean checkUsernamesOnline(String[] usernames) {
        for (int i = 0; i < usernames.length; i++) {
            if (!checkUsernameOnline(usernames[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("running");

        try {
            ServerSocket server = new ServerSocket(8091);
            System.out.println("Waiting for clients...");
            while (true) {
                ConnectedClient client = new ConnectedClient(server.accept());
                System.out.println("new Client connected.");
                client.start();
            }
        } catch (IOException ex) {
            Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
